package me.shibor.study.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 例子：
 * Servlet读取根目录文件信息
 * @author shibor
 *
 */
@WebServlet("/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public ServletDemo3() {
        super();
    }



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		ServletContext servletContext = request.getServletContext();
		InputStream resourceAsStream = servletContext.getResourceAsStream("/test.properties");
		Properties pros = new Properties();
		
		pros.load(resourceAsStream);
		
		String property = pros.getProperty("serverName");
		System.out.println("serverName: " + property);
		
		
		InputStream classResourceAsStream = servletContext.getClassLoader().getResourceAsStream("/test_class.properties");
		Properties classPros = new Properties();
		classPros.load(classResourceAsStream);
		property = classPros.getProperty("key");
		System.out.println("key: " + property);
		
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
